import jsonrpc from "@/libs/jsonrpc";
import { setToken, getToken } from '@/libs/util';
export default {
    state: {
        // 自动登录的id
        paticipantID: undefined,
        // cookie中的自动登录id，用来判断是否已登录
        token: getToken(),
        // 当前用户状态
        currentUser: undefined,
        // 当前用户权限列表
        access: [],
        // 是否管理员
        is_admin: false,
        // 是否已经在自动登录时重新获取用户状态
        hasGetInfo: false,

        profile: undefined,

        appTokens : {}
    },
    mutations: {
        login(state, result) {
            if (result.id) {
                window.localStorage.setItem("backend.paticipant.id", result.id)
                state.paticipantID = result.id
                state.token = result.id
                setToken(result.id)
            }
            if (result.key) {
                window.localStorage.setItem("backend.paticipant.key", result.key)
            }
            if (result.user) {
                state.currentUser = result.user
                state.access = result.user.access
                state.is_admin = result.user.is_admin
            }
            if (result.ou) {
                state.currentOU = result.ou
            }
        },
        logout(state) {
            window.localStorage.setItem("backend.paticipant.id", "")
            window.localStorage.setItem("backend.paticipant.key", "")
            state.paticipantID = ""
            state.token = ""
            setToken("")
            state.currentUser = null
            state.access = []
            state.currentOU = null
        },
        remote_login(state, result) {
            if (result.id && result.key && result.scene) {
                window.localStorage.setItem("backend.paticipant.id." + result.scene, result.id)
                window.localStorage.setItem("backend.paticipant.key." + result.scene, result.key)
                state.token = result.id
                setToken(result.id)
            }
        },
        setToken(state, token) {
            state.token = token
            setToken(token)
        },

        setHasGetInfo(state, status) {
            state.hasGetInfo = status
        }
    },
    actions: {
        // 登录
        handleLogin({ commit }, { userName, password }) {
            userName = userName.trim()
            return new Promise((resolve, reject) => {
                jsonrpc("backend@user.login", [{
                    account: userName,
                    password: password
                }], (err, res) => {
                    if (err) {
                        commit("handleError", err)
                        reject(err)
                    } else {
                        commit("login", res)
                        commit("finishLoading")
                        resolve(res)
                    }
                })
            })
        },
        // 退出登录
        handleLogOut({ state, commit }) {
            return new Promise((resolve, reject) => {
                commit("setTagNavList", [])
                commit("logout")
                resolve()
            })
        },
        // 获取用户相关信息
        getUserInfo({ state, commit }) {
            commit("startLoading")
            return new Promise((resolve, reject) => {
                if (!state.currentUser) {
                    jsonrpc.doParticipant((err, res) => {
                        if (err) {
                            commit("handleError", err)
                            commit("logout")
                            reject(err)
                        } else {
                            commit("login", res)
                            commit("setHasGetInfo", true)
                            commit("finishLoading")
                            resolve(state.currentUser)
                        }
                    })
                } else {
                    commit("finishLoading")
                    resolve(state.currentUser)
                }
            })
        }
    }
}